﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Urbania.Utilities
{
    public class MouseWrap
    {

        public const int LeftClick = 0;
        public const int RightClick = 1;
        public const int MiddleClick = 2;
        public static int ScrollWheelValue;
        private static MouseState mouseState;
        private static Vector2 mousePosScreen;
        private static Vector2 mousePosWorld;
        private static bool[] isDownClicked;
        private static bool[] isDownPrev;

        public static Vector2 PositionScreen { get { return mousePosScreen; } }
        public static Vector2 PositionWorld { get { return mousePosWorld; } }
        public static MouseState MouseState { get { return mouseState; } }


        public static void InitMouse()
        {
            isDownClicked = new bool[3];
            isDownPrev = new bool[3];
        }

        public static void Update()
        {
            mouseState = Mouse.GetState();
            mousePosScreen.X = mouseState.X;
            mousePosScreen.Y = mouseState.Y;

            ScrollWheelValue = mouseState.ScrollWheelValue;
            isDownPrev[LeftClick] = isDownClicked[LeftClick];
            isDownPrev[RightClick] = isDownClicked[RightClick];
            isDownPrev[MiddleClick] = isDownClicked[MiddleClick];
            isDownClicked[LeftClick] = (mouseState.LeftButton == ButtonState.Pressed);
            isDownClicked[RightClick] = (mouseState.RightButton == ButtonState.Pressed);
            isDownClicked[MiddleClick] = (mouseState.MiddleButton == ButtonState.Pressed);
        }

        public static void Update(GraphicsDevice graphicsDevice, Camera cam)
        {
            mouseState = Mouse.GetState();
            mousePosScreen.X = mouseState.X;
            mousePosScreen.Y = mouseState.Y;

            mousePosWorld.X = cam.Position.X + ((mousePosScreen.X / graphicsDevice.Viewport.Width) - 0.5f) * cam.ScreenZoomed.X;
            mousePosWorld.Y = cam.Position.Y + ((mousePosScreen.Y / graphicsDevice.Viewport.Height) - 0.5f) * cam.ScreenZoomed.Y;
            ScrollWheelValue = mouseState.ScrollWheelValue;
            isDownPrev[LeftClick] = isDownClicked[LeftClick];
            isDownPrev[RightClick] = isDownClicked[RightClick];
            isDownPrev[MiddleClick] = isDownClicked[MiddleClick];
            isDownClicked[LeftClick] = (mouseState.LeftButton == ButtonState.Pressed);
            isDownClicked[RightClick] = (mouseState.RightButton == ButtonState.Pressed);
            isDownClicked[MiddleClick] = (mouseState.MiddleButton == ButtonState.Pressed);
        }

        public static bool isDown(int ButtonConst)
        {
            if (ButtonConst < 0 || ButtonConst > 2)
            {
                return false;
            }
            else
            {
                return isDownClicked[ButtonConst];
            }
        }

        public static bool wasPressed(int ButtonConst)
        {
            if (ButtonConst < 0 || ButtonConst > 2)
            {
                return false;
            }
            else
            {
                return isDownClicked[ButtonConst] && !isDownPrev[ButtonConst];
            }
        }

        public static bool wasReleased(int ButtonConst)
        {
            if (ButtonConst < 0 || ButtonConst > 2)
            {
                return false;
            }
            else
            {
                return !isDownClicked[ButtonConst] && isDownPrev[ButtonConst];
            }
        }

    }
}
